package com.cy.db.sys.service.impl;

import com.cy.db.common.exception.ServiceException;
import com.cy.db.common.pojo.PageObject;
import com.cy.db.sys.dao.SysLogDao;
import com.cy.db.sys.entity.SysLog;
import com.cy.db.sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;
    @Override
    public PageObject<SysLog> findPageObjects(String username, Long pageCurrent) {
        //1.验证参数的有效性
        if(pageCurrent==null || pageCurrent<1){
                throw new IllegalArgumentException("当前页码值无效");
        }
//        Optional.ofNullable(pageCurrent).orElseThrow(()->{
//            return new IllegalArgumentException("当前页码值无效");
//        });
        //2.统计总记录数并进行校验
        Long rowCount = sysLogDao.getRowCount(username);
        if(rowCount==0){
            throw new ServiceException("没有找到对应的记录");
        }
        //3.基于条件查询当前页记录(pageSize定义为5)
        //3.1定义pageSize
        int pageSize = 5;
        //3.2计算startIndex
        long startIndex = (pageCurrent-1)*pageSize;
        //3.3执行当前数据的查询操作
        List<SysLog> records = sysLogDao.findPageObjects(username, startIndex, pageSize);
        return new PageObject<>(rowCount,records,pageSize,pageCurrent);
    }

    @Override
    public int deleteObjects(Integer... ids) {
        int row = sysLogDao.deleteObjects(ids);
        return row;
    }
}
